package is.web.table;

import is.interfaces.Bean;
import is.interfaces.Table;
import is.model.exceptions.EmptyValueException;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author aspr0
 */
public abstract class AbstractTable<Datatype extends Bean> implements Table<Datatype>
{
    protected List<Datatype> beans;
    protected Datatype initialBean;
    protected Integer orderBy;
    protected final String ARROW_UP = " ↑";
    protected Boolean hasSearched = false;

    @Override
    public String getDatatypeName()
    {
        return initialBean.getTableName();
    }

    @Override
    public void setInitialBean(Datatype initialBean)
    {
        this.initialBean = initialBean;
    }

    @Override
    public void setHasSearched(Boolean hasSearched)
    {
        this.hasSearched = hasSearched;
    }

    @Override
    public void setBeans(List<Datatype> beans)
    {
        this.beans = beans;
    }

    @Override
    public Integer getOrderBy()
    {
        return orderBy;
    }

    @Override
    public void setOrderBy(String orderBy)
    {
        try
        {
            this.orderBy = Integer.parseInt(orderBy);

            if (this.orderBy < 0 || this.orderBy > initialBean.getFields().length - 1)
            {
                this.orderBy = -1;
            }
        }
        catch (Exception e)
        {
            this.orderBy = -1;
        }
    }

    @Override
    public abstract String genTable() throws EmptyValueException, SQLException;
}
